<?php

/**
 * 签到
 * Class sign
 */
class sign {

    private $_auth;
    private $db_sign;

    public function  __construct() {
        $this->_auth = param::get_cookie('auth');
        $this->db_sign = pc_base::load_model('sign_model');

        if (!$this->_auth) {
            json_exit('请先登录', 201);
        }
    }

    //签到
    public function doit() {
        $uid = (int)param::get_cookie('_userid');
        $ymd = date('Y-m-d');

        //查是否已有
        $sql = "uid='$uid' and ymd='$ymd'";
        $res = $this->db_sign->get_one($sql);

        if ($res) {
            json_exit(null, 200, '每天只能签到一次');
        } else {
            //记录
            $this->db_sign->insert([
                'uid' => $uid,
                'ymd' => date('Y-m-d'),
                'ymdhis' => date('Y-m-d H:i:s'),
            ]);

            //加分
            $db_member = pc_base::load_model('member_model');
            $cha = $db_member::log($uid, 10, $db_member::TYPE_SIGN, "签到", __METHOD__);

            json_exit(10, 100, '签到成功');
        }
    }

    //是否
    public function is() {
        $uid = (int)param::get_cookie('_userid');
        $ymd = date('Y-m-d');

        //查是否已有
        $sql = "uid='$uid' and ymd='$ymd'";
        $res = $this->db_sign->get_one($sql);
        if ($res) {
            json_exit(null, 200);
        } else {
            json_exit(null, 100);
        }
    }

}